package com.tanhua.appserver.service;

import com.tanhua.appserver.exception.BusinessException;
import com.tanhua.appserver.hanlder.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbointerface.api.CommentApi;
import com.tanhua.dubbointerface.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.utills.CommentType;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentsService {

    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 提交评论操作
     * @param movementId 动态编号
     * @param content 评论
     */
    public void commitComments(String movementId, String content) {
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setContent(content);
        comment.setCommentType(CommentType.COMMENT.getType());
        comment.setUserId(userId);
        commentApi.save(comment,movementId);


    }

    public Integer like(String id) {
        //判断是否已经存在点赞信息
        boolean flag =commentApi.hasComment(id,UserHolder.getUserId(),CommentType.LIKE);
        if (!flag){
            //如果存在则抛出异常
            throw new BusinessException(ErrorResult.likeError());
        }
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setUserId(userId);
        String key = Constants.MOVEMENTS_INTERACT_KEY+id;
        String hashKey=Constants.MOVEMENT_LIKE_HASHKEY+UserHolder.getUserId();
        redisTemplate.opsForHash().put(key,hashKey,"1");
        return commentApi.saveCommentLike(comment,id);
    }
    public Integer disLike(String id){
        //判断是否已经存在点赞信息
        boolean flag =commentApi.hasComment(id,UserHolder.getUserId(),CommentType.LIKE);
        if (!flag){
            //如果不存在则抛出异常
            throw new BusinessException(ErrorResult.disLikeError());
        }
        Long userId = UserHolder.getUserId();
        Comment comment = new Comment();
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setUserId(userId);
        String key = Constants.MOVEMENTS_INTERACT_KEY+id;
        String hashKey=Constants.MOVEMENT_LIKE_HASHKEY+UserHolder.getUserId();
        redisTemplate.opsForHash().delete(key,hashKey);
        return commentApi.deleteCommentLike(comment,id);
    }

    /**
     * 获取动态的评论数据
     * @param movementId
     * @param page
     * @param pagesize
     * @return
     */
    public PageResult getPage(String movementId, int page, int pagesize) {
        //通过动态id获取评论和点赞数据
        List<Comment> comment=commentApi.findByPublishId(movementId,CommentType.COMMENT.getType(),page,pagesize);
        //判断返回的集合是否为空
        if (comment==null){
            //为空返回一个空的数据
            return new PageResult();
        }
        //将得到的comment集合进行流操作，封装出CommentVo集合
        List<CommentVo> list=comment.stream().map(item ->{

            UserInfo info = userInfoApi.findUserInfoById(item.getUserId());
            if (info!=null){
                return CommentVo.init(info,item);
            }
            return null;
        }).collect(Collectors.toList());
        //返回PageResult结果
        return new PageResult(page,pagesize,0l,list);
    }


}
